package cn.outsourcing.supermarket.provider;

import cn.outsourcing.supermarket.common.constants.BackstageGoodsConstants;
import cn.outsourcing.supermarket.common.utils.DataUtils;
import org.apache.ibatis.jdbc.SQL;

import java.util.Objects;

/**
 * 后台商品SQL供应器
 *
 * @author gzkemays
 * @since 2021/11/3 20:21
 */
public class BackstageGoodsProvider {
  /**
   * 分页获取全部商品
   *
   * @return 分页查询全部商品
   */
  public String getAllGoods(String name, String mode, Integer sort) {
    return new SQL() {
      {
        SELECT("hg.*", "hc.name as `categoryName`", "hp.*")
            .FROM("hiolabs_goods hg")
            .LEFT_OUTER_JOIN(
                "hiolabs_category hc on hg.category_id = hc.id",
                "hiolabs_product hp on hp.goods_id = hg.id");
        if (Objects.nonNull(name)) {
          String likeName = name.trim();
          WHERE("hg.name like concat('%','" + likeName + "','%')");
          if (DataUtils.isNumberByStr(likeName)) {
            OR().WHERE("hg.id like concat('%','" + likeName + "','%')");
          }
        }
        AND().WHERE("hg.is_delete = 0").AND().WHERE("hp.is_delete = 0");
        if (mode.equals(BackstageGoodsConstants.DROP_GOODS)) {
          WHERE("hg.is_on_sale = 0");
        }
        if (mode.equals(BackstageGoodsConstants.ON_SALE_GOODS)) {
          WHERE("hg.is_on_sale = 1");
        }
        if (mode.equals(BackstageGoodsConstants.OUT_GOODS)) {
          WHERE("hg.goods_number = 0");
        }
        if (sort.equals(BackstageGoodsConstants.SORT_DEFAULT)) {
          ORDER_BY("hg.sort_order ASC");
        }
        if (sort.equals(BackstageGoodsConstants.SORT_VOLUME)) {
          ORDER_BY("hg.sell_volume DESC");
        }
        if (sort.equals(BackstageGoodsConstants.SORT_GOLD)) {
          ORDER_BY("hg.min_retail_price DESC");
        }
        if (sort.equals(BackstageGoodsConstants.SORT_STOCK)) {
          ORDER_BY("hg.goods_number DESC");
        }
      }
    }.toString();
  }
}
